<template>
    <div id="bankLimitAdd" class="main-container mt10">
        <div>
            <el-form ref="submitForm" :model="submitForm" class="formEmbedTable" :rules="rules" >
            <table>
                <caption>Modify</caption>
                <tr>
                    <td>Account No.：</td>
                    <td>
                        <el-form-item prop="accNo">{{submitForm.accNo}}</el-form-item>
                    </td>
                    <td>Custom No.：</td>
                    <td >
                        <el-form-item prop="accName">{{submitForm.cstNo}}</el-form-item>
                    </td>      			
                </tr>
                <tr>
                    <td >Limit Type：</td>
                    <td colspan='3'>
                        <el-form-item prop="limitType">
                            <el-radio-group v-model="submitForm.limitType">
                                <el-radio class="radio" label="P">HKD Bill Payment Cycle Limit</el-radio>
                                <el-radio class="radio" label="T">Transfer Cycle Limit</el-radio>
                                <el-radio class="radio" label="F">Fixed Deposit Cycle Limit</el-radio>
                            </el-radio-group>
                        </el-form-item>
                    </td>      			
                </tr>
                <tr>
                    <td>Currency：</td>
                     <td colspan='3'>
                        <el-form-item prop="currType">
                            <el-radio-group v-model="submitForm.currType">
                                <el-radio class="radio" label="CNY">Chinese Currency</el-radio>
                                <el-radio class="radio" label="HKD">Hongkong Currency</el-radio>
                                <el-radio class="radio" label="USD">dollor</el-radio>
                            </el-radio-group>
                        </el-form-item>
                    </td>   
                </tr>
                <tr>
                    <td>Channel：</td>
                    <td>
                        <el-form-item prop="paramChannel">
                            <el-select v-model="submitForm.paramChannel" clearable>
                                <el-option v-for="item in paramChannels" :label="item.label" :value="item.value" :key="item.value"></el-option>
                            </el-select>
                        </el-form-item>
                    </td> 
                    <td>custom type：</td>
                    <td>
                        <el-form-item prop="cstType">
                           <el-radio-group v-model="submitForm.cstType">
                                <el-radio class="radio" label="0">private</el-radio>
                                <el-radio class="radio" label="1">Company</el-radio>
                           </el-radio-group>
                        </el-form-item>
                    </td>
                        			
                </tr>
                <tr>
                    <td>Single Limit：</td>
                    <td>
                        <el-form-item prop="limitSingle">
                            <ynet-currency-input v-model="submitForm.limitSingle" name="limitSingle" placeholder="limitSingle" :showChinese="false"></ynet-currency-input>
                                <template>
                                    <span>{{$tools.dict.CRYTrans(submitForm.CRY)}}</span>
                                </template>
                    <!--       <el-input v-model="form.singleLimit"></el-input>  -->
                        </el-form-item>
                    </td> 
                    <td >Daily Limit：</td>
                    <td >
                        <el-form-item prop="limitDay">
                            <ynet-currency-input v-model="submitForm.limitDay" name="limitDay" placeholder="Daily Limit" :showChinese="false"></ynet-currency-input>
                                <template>
                                    <span>{{$tools.dict.CRYTrans(submitForm.CRY)}}</span>
                                </template>
                          
                        </el-form-item>
                    </td>      			
                </tr>
                <tr>
                    <td>Daily amount Limit：</td>
                    <td>
                        <el-form-item prop="dayNum">
                           <el-input v-model="submitForm.dayNum"></el-input>
                        </el-form-item>
                    </td> 
                    <td >Year Limit：</td>
                    <td >
                        <el-form-item prop="limitYear">
                            <ynet-currency-input v-model="submitForm.limitYear" name="limitYear" placeholder="年累计限额" :showChinese="false"></ynet-currency-input>
                                <template>
                                    <span>{{$tools.dict.CRYTrans(submitForm.CRY)}}</span>
                                </template>
                        </el-form-item>
                    </td>      			
                </tr>
            </table>
		    </el-form>
        </div>
        <div class="btnArea mt20 tac">
            <el-button type="primary" @click="validForm('submitForm')">Confirm</el-button>
            <el-button  @click="back">Cancel</el-button>
        </div>
        <!--授权确认页面弹窗-->
        <div>
            <ynet-authDialog ref="dialogTable"  @getAuthForm="getAuthForm"></ynet-authDialog>
        </div>
    </div>
</template>
<script>
 import ghbIcon from '@src/components/icon'
    export default {
        name:'bankLimitAdd',
        components:{
            //ghbIcon
        },
        props:['editForm'],
        mounted(){
            this.submitForm=$.extend({},this.submitForm,this.editForm)
            console.log("submitForm:"+JSON.stringify(this.submitForm))
        },
        data(){
            return{
                paramChannels:[
                    {label:'Phone Bank',value:'CPB'},
                    {label:'Enterprise online banking',value:'CCB'},
                ],
                cstTypes:[
                    {label:'Person',value:'0'},
                    {label:'Company',value:'1'},
                ],
                queryForm:{
                    paramChannel:'',paramCode:'',bsnCode:'IM0201'
                },
                rules:{
                    currType:[
                        { required: true, message: 'required', trigger: 'blur' },
                    ],
                    paramChannel:[
                        { required: true, message: 'required', trigger: 'blur' },
                    ],
                    cstType:[
                        { required: true, message: 'required', trigger: 'blur' },
                    ],
                    bankLimitType:[
                        { required: true, message: 'required', trigger: 'blur' },
                    ],
                    bankLimitSingle:[
                        { validator:this.validatorSingleLimit, trigger: 'blur' },
                    ],
                    bankLimitDay:[
                        { validator:this.validatorDailyCumuLimit, trigger: 'blur' },
                    ],
                    bankDayNum:[
                        { validator:this.validatorCumuDailyCount, trigger: 'blur' },
                    ],
                    bankLimitYear:[
                        { validator:this.validatorCumuYearLimit, trigger: 'blur' },
                    ],
                },
                submitForm:{//提交给后台的数据
                    currType:'',paramChannel:'',limitType:'',cstType:'',limitSingle:'',limitDay:'',dayNum:'',limitYear:'',userNo:'0000001',
                    actionType:'1',bsnCode:'IM0501',accNo:'',cstNo:'',
                },
                needAuth:'',
                authType:'',
            }
        },
        methods:{
             getAuthForm(val){
                //获取授权数据
                //this.authForm=val;
                console.log("val:"+JSON.stringify(val))
                this.submitForm=$.extend(this.submitForm,val)
                this.submit()  //自己页面提交给后台的.do方法
            },
            validForm(form) {
                let _this = this
                console.log("_this.form",JSON.stringify(_this.form))
                this.$refs[form].validate((valid) => {
                if (valid) {
                    if( Number(_this.submitForm.bankLimitSingle) > Number(_this.submitForm.bankLimitDay)) {
                        console.log(' _this.submitForm.banklimitSingle', _this.submitForm.bankLimitSingle)
                        console.log(' _this.submitForm.bankLimitDay', _this.submitForm.bankLimitDay)
                        _this.$alert('single limit more than day limit',  {
                            confirmButtonText: 'Confirm'
                        })
                        return
                    }
                    if ( Number(_this.submitForm.bankLimitDay) > Number(_this.submitForm.bankLimitYear)) {
                        _this.$alert('single limit more than year limit', {
                                confirmButtonText: 'Confirm'
                            })
                        return
                    }
                    _this.queryAuth()
                } else {
                        console.log('error submit!!');
                        return false;
                }
                });
            },
            queryAuth(){//查询授权模式
                let body={};body.bsnCode="IM0501";body.version="IM0501",
                this.$tools.request(this,"IM010001Action.do",body).then(data=>{
                    if(data.body.errorCode==0){
                        if(data.body.needAuth==0){//无需授权
                            this.submit()
                        }else if(data.body.needAuth==1 && data.body.authType==1){
                            this.needAuth=1;
                            this.authType=1;
                            this.$refs["dialogTable"].show()
                                    // this.dialogTableVisible=true;
                        }else if( data.body.needAuth==1 && data.body.authType==2 ){
                             this.needAuth=1;
                            this.authType=2;
                            this.submit()
                        }else{
                            this.$alert('Authorization Type Error', {
                                confirmButtonText: 'Confirm',
                                callback: action => {
                                }
                            });
                        }
                    }else{
                        this.$alert("System Error",{
                            confirmButtonText: 'Confirm',
                            callback: action => {
                            }
                        })
                    }
                })   
			},
            submit(){
                this.$tools.request(this,"IM0501Action.do",this.submitForm).then(data=>{
                    console.log("data:"+JSON.stringify(data))
                    if(data.body.errorCode==0){
                        if(this.needAuth==1 && this.authType==2){
                            this.$message({
								type:'success',
								message:'Waiting for Authorized'
							})
                            this.$emit("back")
                        } else if(this.needAuth==1 && this.authType==1){
                            if(data.body.authStatus==1){
                                this.$message({
                                    type:'warning',
                                    message:'Authorship No. or password wrong ,please resume load',
                                    showClose:true,
                                })
                                this.$emit("back")
                            }else{
                                 this.$message({
                                    type:'success',
                                    message:'Add Success',
                                    showClose:true,
                                })
                                this.$emit("back")
                            }
                        }else{
                            this.$message({
                                type:'success',
                                message:'Add Success',
                                showClose:true,
                            })
                            this.$emit("back")
                        }
                        
                    }
                })
            },
            query(){//返回到查询页面
                this.$emit('query',this.queryForm)
            },
            back(){
                this.$emit("back")
            },
            validatorSingleLimit(rule, value, callback){
                let _this=this
                if("" == value){
                    return callback("required")
                }
                if((!('0' == value)) && (!(/(^[1-9][0-9]{0,11}$)|(^[1-9][0-9]{0,11}\.\d{1,2}$)|(^[0-9]\.\d{1,2}$)/.test(value)))) {
                    return callback("format error")
                }
                callback()
            },
                //表单日累计限额校验
            validatorDailyCumuLimit(rule, value, callback){
                let _this=this
                if("" == value){
                    return callback("required")
                }
                callback()
            },
            //表单日累计笔数校验
            validatorCumuDailyCount(rule, value, callback){
                let _this=this
                if("" == value){
                    return callback("required")
                }
                if((!('0' == value)) && (!(/^[1-9]\d*$/.test(value)))) {
                    return callback("format error")
                }
                callback()
            },
                //表单年累计限额校验
            validatorCumuYearLimit(rule, value, callback){
                let _this=this
                if("" == value){
                    return callback("required")
                }
                callback()
            },
        }
    }
</script>